package com.atguigu.gmall.geteway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;

@Configuration
public class CorsConfig {

    @Bean
    public CorsWebFilter corsWebFilter(){
        // 初始化一个配置类
        CorsConfiguration configuration = new CorsConfiguration();
        // 允许跨域访问的域名。*-允许所有，建议不要写*，1-不够安全 2-不能携带cookie
        configuration.addAllowedOrigin("http://manager.gmall.com");
        configuration.addAllowedOrigin("http://localhost:1000");
        // 允许跨域访问的方法
        configuration.addAllowedMethod("*");
        // 允许携带的请求头信息
        configuration.addAllowedHeader("*");
        // 是否允许携带cookie
        configuration.setAllowCredentials(true);

        UrlBasedCorsConfigurationSource configurationSource = new UrlBasedCorsConfigurationSource();
        configurationSource.registerCorsConfiguration("/**", configuration);
        return new CorsWebFilter(configurationSource);
    }
}